﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using System.Diagnostics;
using Microsoft.Phone.Controls.Maps;
using unifiqAnthony.RouteService;
using System.Collections.ObjectModel;
using Microsoft.Phone.Tasks;

namespace unifiqAnthony
{
    public partial class ShowRoute : PhoneApplicationPage
    {
        private GeoCoordinateWatcher watcher = null;
        internal const string id = "AoetA0820tVc4COdo9bGyEKy0mBSDjoSeb9vlIRP3iC3RWibRFlwOGPGVLOUn7Fs";
        private readonly CredentialsProvider _credentialsProvider = new ApplicationIdCredentialsProvider(id);
        private GeoCoordinate startLocation = new GeoCoordinate(1.37998776553479, 103.848916053772), endLocation;
        private string endLocationName;
        private Pushpin startPin, endPin;

        public CredentialsProvider CredentialsProvider
        {
            get { return _credentialsProvider; }
        }

        public ShowRoute()
        {
            InitializeComponent();

            //GPS Service
            /*if (watcher == null)
            {
                watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // Use high accuracy. 
                watcher.MovementThreshold = 20; // Use MovementThreshold to ignore noise in the signal. 
                watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            }
            watcher.Start(); 
            */
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            endLocationName = NavigationContext.QueryString["location"];
            double lat = Convert.ToDouble(NavigationContext.QueryString["lat"]);
            double lng = Convert.ToDouble(NavigationContext.QueryString["lng"]);

            endLocation = new GeoCoordinate(lat, lng);

            startPin = new Pushpin();
            startPin.Location = startLocation;
            startPin.Template = this.Resources["start_marker"] as ControlTemplate;

            endPin = new Pushpin();
            endPin.Location = endLocation;
            endPin.Template = this.Resources["end_marker"] as ControlTemplate;

            MapLayer layer = new MapLayer();
            routeMap.Center = endLocation;
            routeMap.ZoomLevel = 13;
            layer.Children.Add(startPin);
            layer.Children.Add(endPin);
            routeMap.Children.Add(layer); 
        }

        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e) 
        { 
            if (e.Status == GeoPositionStatus.Ready) 
            { 
                startPin.Location = watcher.Position.Location;
                watcher.Stop(); 
            } 
        }

        private void routeBtn_Click(object sender, RoutedEventArgs e)
        {
            BingMapsDirectionsTask directionTask = new BingMapsDirectionsTask();
            directionTask.Start = new LabeledMapLocation("Your start location", startLocation);
            directionTask.End = new LabeledMapLocation(endLocationName, endLocation);
            directionTask.Show();
        } 

    }
}